package com.novax.ex.activity.provider.controller;

import com.novax.ex.activity.infrastructure.entity.MallPrize;
import com.novax.ex.activity.infrastructure.entity.MallPrizeMemos;
import com.novax.ex.activity.infrastructure.entity.MallPrizeOrder;
import com.novax.ex.activity.open.api.inner.MallAdjustmentInnerApi;
import com.novax.ex.activity.open.model.query.MallAdjustmentHistoryQuery;
import com.novax.ex.activity.open.model.response.MallAdjustmentHistoryResponse;
import com.novax.ex.activity.open.model.response.MallAdjustmentMemoResponse;
import com.novax.ex.activity.open.model.response.MallAdjustmentResponse;
import com.novax.ex.activity.provider.service.MallPrizeMemosService;
import com.novax.ex.activity.provider.service.MallPrizeOrderService;
import com.novax.ex.activity.provider.service.MallPrizeService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.enums.MallPrizeStatus;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageQuery;
import com.novax.ex.common.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 结果调整
 *
 * @author Deucalion@novax.email
 * @date 2023/4/3 10:21
 */
@Slf4j
@RestController
public class MallAdjustmentInnerController extends BaseActivityController implements MallAdjustmentInnerApi {
    @Resource
    private MallPrizeService mallPrizeService;
    @Resource
    private MallPrizeOrderService mallPrizeOrderService;
    @Resource
    private MallPrizeMemosService mallPrizeMemosService;
    @Override
    public ReturnResult<PageUtil<MallAdjustmentResponse>> list(String language, PageQuery query) {
        PageUtil<MallAdjustmentResponse> pages = new PageUtil<>(query);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", query);
        pages.setCondition(condition);
        mallPrizeService.findAdjustmentPage(pages,MallAdjustmentResponse.class);
        return ReturnResult.success(pages);
    }

    @Override
    public ReturnResult<PageUtil<MallAdjustmentHistoryResponse>> history(String language, MallAdjustmentHistoryQuery query) {
        PageUtil<MallAdjustmentHistoryResponse> pages = new PageUtil<>(query);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", query);
        pages.setCondition(condition);
        mallPrizeService.findHistoryPage(pages,MallAdjustmentHistoryResponse.class);
        return ReturnResult.success(pages);
    }

    @Override
    public ReturnResult<List<MallAdjustmentMemoResponse>> memo(String language,Long mallPrizeId) {
        Map<String, Object> param = new HashMap<>();
        param.put("mallPrizeId",mallPrizeId);
        List<MallPrizeMemos> list = mallPrizeMemosService.findList(param);
        if(list.size() == 0){
            return ReturnResult.success(new ArrayList<>());
        }else{
            return ReturnResult.success(CopyUtils.copyList(list, MallAdjustmentMemoResponse.class));
        }
    }

    @Override
    public ReturnResult appoint(String language, Long adminId, Long orderId, Long userId) {
        if(null == orderId || null == userId){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        MallPrizeOrder prizeOrder = mallPrizeOrderService.find(orderId);
        if(null == prizeOrder){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        MallPrize prize = mallPrizeService.find(prizeOrder.getMallPrizeId());
        if(null == prize){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        if(!MallPrizeStatus.start.getStatus().equals(prize.getStatus())){
            return ReturnResult.fail(getInternational(language, InternationalCode.CANNOT_OPERATION));
        }
        boolean result = mallPrizeMemosService.appoint(adminId,userId,prizeOrder.getMallPrizeId());
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult cancelAppoint(String language, Long adminId, Long mallPrizeId) {
        if(null == mallPrizeId){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        MallPrize prize = mallPrizeService.find(mallPrizeId);
        if(null == prize){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        if(!MallPrizeStatus.start.getStatus().equals(prize.getStatus())){
            return ReturnResult.fail(getInternational(language, InternationalCode.CANNOT_OPERATION));
        }
        boolean result = mallPrizeMemosService.cancelAppoint(adminId,mallPrizeId);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }
}
